<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- 头部 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20"> <!-- :span:20 表示一列占20份 -->
            <svg-icon icon-class="zip" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4"> <!-- :span:4 表示一列占4份 -->
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="clickAddOrEdit('',false)">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 内容 -->
        <!-- :props="defaultProps"  在此处用于定义label 和 children-->
        <!-- 默认树形 全部展开 default-expand-all -->
        <el-tree :data="list" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20"> <!-- :span:20 表示一列占20份 -->
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4"> <!-- :span:4 表示一列占4份 -->
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="clickAddOrEdit(data.id,false)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="clickAddOrEdit(data.id,true)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="!data.children.length" @click.native="clickDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- 关闭 -->
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="showAddOrEdit"
      width="30%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!-- 表单内容 -->
      <AppOrEdit v-if="showAddOrEdit" :id="curId" :is-edit="isEdit" :origin-list="originList" @closeDialog="addOrEditSuccess">
        <span slot="footer" class="dialog-footer" />
      </apporedit></el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/department'
import { tranListToTreeData } from '@/utils'
import AppOrEdit from './deptDialog.vue'

export default {
  components: { AppOrEdit },

  data() {
    return {
      list: [],
      // 是否显示dialog
      showAddOrEdit: false,
      // 当前部门id
      curId: '',
      // 是否是编辑状态
      isEdit: false,
      // 校验需要得信息
      originList: []

      // defaultProps 默认为如下所示 , 所以不要也罢
      // defaultProps: {
      //   children: 'children',
      //   label: 'label' // label为固定名 , 'label'为变量名 , 所以要更改list里label名为name , 就需要在此处将'label'改为'name'
      // }
    }
  },
  created() {
    // 更新数据
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        res.data.depts.shift()
        // 用工具函数把 平铺的数组转成 树型数据
        this.list = tranListToTreeData(res.data.depts)

        // this.originList = res.data.depts.map(item => {
        //   return {
        //     id: item, pid: item.pid, code: item.code, name: item.name
        //   }
        // })

        // this.originList = res.data.depts.map(({ id, pid, code, name }) => { return { id, pid, code, name } })

        this.originList = res.data.depts.map(({ id, pid, code, name }) => ({ id, pid, code, name }))
        // console.log(this.originList)
      } catch (err) {
        console.log(err)
      }
    },
    clickAddOrEdit(id, isEdit) {
      if (this.isEdit) {
        this.showAddOrEdit = true
        // console.log(pid)
        // 获取pid之后 父向子传值
        this.curId = id
        this.isEdit = isEdit
      } else {
        this.showAddOrEdit = true
        this.curId = id
        this.isEdit = isEdit
      }
    },
    // clickAdd(id) {
    //   this.showAddOrEdit = true
    //   // console.log(pid)
    //   // 获取pid之后 父向子传值
    //   this.curId = id
    //   this.isEdit = false
    // },
    // clickEdit(id) {
    //   this.showAddOrEdit = true
    //   this.curId = id
    //   this.isEdit = true
    // },
    addOrEditSuccess(success) {
      if (success) {
        this.showAddOrEdit = false
        this.loadDepartments()
      } else {
        this.showAddOrEdit = false
      }

      // console.log(success)
    },
    // 点击删除
    clickDel(id) {
      this.$confirm('确定删除该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await delDepartment(id)
          // 删除之后重新加载数据
          this.loadDepartments()
          this.$message({
            type: 'success',
            message: res.message
          })
        } catch (error) {
          this.$message.error(error.message)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
